package com.zzb.weikeautosign;

import com.alibaba.fastjson.JSONObject;
import com.zzb.weikeautosign.param.BaseParam;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * http任务执行公共方法
 * Created by zzb on 16/1/18.
 */
public class HttpExcuteUtil {
    /**
     * httpClient的实现方式
     * @param url
     * @param param
     * @return
     */
    public static JSONObject execute(String url , BaseParam param){
        JSONObject jsonObject = new JSONObject();

        HttpClient httpClient = new DefaultHttpClient();
        url = url + "?" + param.toParam();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Accept-Encoding","gzip");
        httpPost.setHeader("User-Agent","Dalvik/1.6.0 (Linux; U; Android 4.2.2; MI Build/JDQ39E)");
        try {
            HttpResponse response = httpClient.execute(httpPost);
            if(response.getStatusLine().getStatusCode() == 200){
                String entity = EntityUtils.toString(response.getEntity());
                jsonObject = JSONObject.parseObject(entity);
            }
        } catch (IOException e) {
            e.printStackTrace();
            jsonObject.put("success",-1);
        } finally {
            httpPost.releaseConnection();
        }
        return jsonObject;
    }

    /**
     * 原生的httpUrlConnection方式
     * @param url
     * @param param
     * @return
     */
    public static JSONObject executeConnection(String url , BaseParam param){
        JSONObject jsonObject = new JSONObject();

        try {
            URL urlObject = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) urlObject.openConnection();
            conn.setRequestMethod("POST");
            conn.setConnectTimeout(12000);
            conn.setReadTimeout(12000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestProperty("Accept-Encoding","gzip");
            conn.setRequestProperty("User-Agent","Dalvik/1.6.0 (Linux; U; Android 4.2.2; MI Build/JDQ39E)");
            OutputStream os = conn.getOutputStream();
            os.write(param.toParam().getBytes());
            os.flush();
            if(conn.getResponseCode() == 200){
                InputStream is = conn.getInputStream();
                InputStreamReader isReader = new InputStreamReader(is);
                BufferedReader bufferedReader = new BufferedReader(isReader);
                StringBuilder resultBuilder = new StringBuilder();
                String tmp;
                while ((tmp = bufferedReader.readLine())!=null){
                    resultBuilder.append(tmp);
                }
                jsonObject = JSONObject.parseObject(resultBuilder.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
            jsonObject.put("success","-1");
        }
        return jsonObject;
    }

}
